"""
---------------------------------------------------------------------
- File: python时间模块.py
- Date: 2025/11/14
- Author: yangzhide
- Email: xxzjyzd@126.com
- Description: 
---------------------------------------------------------------------
"""

import time

# ----time模块主要场景----
# 1. 获取当前时间
# 2. 睡眠

# 1. 获取当前时间戳
timestamp = time.time()
# print(timestamp) # 1763115713.9918938

# 2. 睡眠
# time.sleep(2) # 睡眠2秒


import datetime

# ----datetime模块4大核心类主要场景----

# 1. datatime.data 日期
today = datetime.date.today()
# print(today) # 2025-11-14
# 日期计算
next_week = today + datetime.timedelta(days=7)
days_ago = today - datetime.timedelta(days=30)

# 2. datetime.time 时间
now = datetime.datetime.now()
# print(now) # 2025-11-14 15:22:13.991893

# 3. datetime.datetime 日期时间
now1 = datetime.datetime.now()
# print(now) # 2025-11-14 15:22:13.991893

# 解析日期时间字符串
dt_str1 = '2025-11-14 15:22:13'
dt_obj1 = datetime.datetime.strptime(dt_str1, '%Y-%m-%d %H:%M:%S')
# print(dt_obj) # 2025-11-14 15:22:13

# 日期时间格式化
dt_obj2 = datetime.datetime.now()
dt_str = dt_obj2.strftime('%Y-%m-%d %H:%M:%S')
# print(dt_str) # 2025-11-14 15:22:13

# 4. datetime.timedelta 时间差
delta = datetime.timedelta(days=1, hours=2, minutes=3, seconds=4)
# print(delta) # 1 days, 2:03:04
# 获取差值
diff = datetime.datetime.now() - datetime.timedelta(days=1)
# print(diff) # 2025-11-13 15:22:13.991893
